package com.mmlc.meimei.lichengkongjian.mine;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.androidnetframe.BaseParentActivity;
import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.responsejavabean.RequestError;
import com.androidnetframe.utils.LogUtils;
import com.mmlc.inter.InterActivity;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.network.filter.CustomerListRequestFilter;
import com.mmlc.network.responsebean.CustomerListResponseBean;
import com.mmlc.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/11/29.
 */
public class ActivityCustomerChoice extends InterActivity implements View.OnClickListener {

    @InjectView(R.id.tv_title)
    TextView tvTitle;
    @InjectView(R.id.iv_back)
    ImageView ivBack;
    @InjectView(R.id.rl_title)
    RelativeLayout rlTitle;
    @InjectView(R.id.rc_customer)
    RecyclerView mRecyclerView;
    @InjectView(R.id.tv_save)
    TextView tvSave;

    @Override
    protected int initContentView() {
        return R.layout.activity_customer_choice;
    }

    @Override
    protected void initView() {
        ButterKnife.inject(this);
        tvTitle.setText("选择客户");
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        initData();

    }

    @OnClick({R.id.iv_back,R.id.tv_save})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_save:
                choiceinfo();
                break;
        }
    }

    public int numFlag;
    public boolean isChoice;
    private void choiceinfo() {

        if(!isChoice){
            ToastUtils.showToast("未选择客户信息");
            return;
        }
        String customer_phone = datas.get(numFlag).getCustomer_phone();
        String customer_name = datas.get(numFlag).getCustomer_name();
        if(TextUtils.isEmpty(customer_name)||TextUtils.isEmpty(customer_phone)){
            ToastUtils.showToast("未选择客户信息");
            return;
        }
        Intent intent = new Intent();
        intent.putExtra("phone",customer_phone);
        intent.putExtra("name",customer_name);
        setResult(200,intent);
        finish();
    }

    public List<CustomerListResponseBean.DataBean> datas;
    public CusAdapter adapter;

    private void initData() {
        datas= new ArrayList<>();
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new CusAdapter(datas);
        Log.e(TAG, "displayCustomerList: "+datas.size());
        mRecyclerView.setAdapter(adapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        CustomerListRequestFilter filter = new CustomerListRequestFilter((BaseParentActivity) activity);
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<CustomerListResponseBean>() {
            @Override
            public void onSuccess(CustomerListResponseBean result) {
                displayCustomerList(result);
            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                ToastUtils.showToast("数据有误，请重新进入");
            }
        });
    }

    private void displayCustomerList(CustomerListResponseBean result) {
        datas.clear();
        datas.addAll(result.getData());
        adapter.notifyDataSetChanged();
    }

    class CusAdapter extends RecyclerView.Adapter<CusAdapter.CusViewHolder>{

        public List<CustomerListResponseBean.DataBean> datas;

        public CusAdapter(List<CustomerListResponseBean.DataBean> datas) {
            this.datas = datas;
        }

        public List<CheckBox> list_cb = new ArrayList<CheckBox>();
        @Override
        public  CusViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_choice_customer, parent, false);
            CusViewHolder holder = new CusViewHolder(view);
            return holder;
        }

        @Override
        public void onBindViewHolder(CusViewHolder holder, final int position) {
            CustomerListResponseBean.DataBean dataBean = datas.get(position);
            list_cb.add(holder.checkbox);
            if(dataBean.getCustomer_name()!=null) {LogUtils.e("vh.tv_name",holder.tv_name);
                holder.tv_name.setText(dataBean.getCustomer_name());
            }
            if(dataBean.getCustomer_phone()!=null) {
                holder.tv_phone.setText(dataBean.getCustomer_phone());
            }
            holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if(isChecked){
                        numFlag = position;
                        isChoice = true;
                        for (int i = 0; i < list_cb.size(); i++) {
                            if(i == position){

                            }else{
                                list_cb.get(i).setChecked(false);
                            }
                        }
                    }
                }
            });
        }

        @Override
        public int getItemCount() {
            if(datas == null){
                return 0;
            }
            return datas.size();
        }

        class CusViewHolder extends RecyclerView.ViewHolder{
            TextView tv_name,tv_phone;
            CheckBox checkbox;

            public CusViewHolder(View view) {
                super(view);
                tv_name = (TextView) view.findViewById(R.id.tv_name);
                tv_phone = (TextView) view.findViewById(R.id.tv_phone);
                checkbox = (CheckBox) view.findViewById(R.id.cb_check);
            }
        }
    }


}
